<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="商标">
        <span>后台管理系统界面</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <!-- 主题区域  -->
    <el-container>

      <!-- 侧边栏导航区域  -->
      <el-aside :width="isCollapse? '64px':'200px'">
        <!-- 折叠展开按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>

        <el-menu
          background-color="#333744  "
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >

          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="iconfont" :class="iconsObj[item.id]"></i>
              <!-- 文字 -->
              <span>{{item.authName}}</span>
            </template>


            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="savaNavState('/'+subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文字 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>

          </el-submenu>

        </el-menu>
      </el-aside>


      <!-- 商城主体显示区域  -->
      <el-main>
        <router-view></router-view>
      </el-main>


    </el-container>
  </el-container>


</template>

<script>
  export default {
    data() {
      return {
        menulist:[],
        iconsObj:{
          '125':'el-icon-user-solid',
          '103':'el-icon-s-check',
          '101':'el-icon-shopping-cart-full',
          '102':'el-icon-office-building',
          '145':'el-icon-s-data'
        },
        isCollapse:false,
        activePath:''
      }
    },
    created() {
      this.getMenuList();
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      logout() {
        window.sessionStorage.removeItem('token');
        this.$router.push('/login')
      },
      //获取所有的菜单
      async getMenuList() {
        const {data: res } = await this.$http.get('menus')
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        } else {
          this.menulist = res.data
        }
      },
      // 导航菜单折叠收起
      toggleCollapse(){
        this.isCollapse = !this.isCollapse
      },
      //保存二级菜单点击高亮状态
      savaNavState(activePath){
        window.sessionStorage.setItem('activePath',activePath);
        this.activePath = activePath
      }
    },

  }
</script>

<style lang="less" scoped>
  .el-header {
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;

    >div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #333744;

    .toggle-button {
      background-color: #4A5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }

    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #EAEDF1;
  }

  .home-container {
    height: 100%;
  }

  .iconfont {
    margin-right: 15px;
  }
</style>